<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="/layui/css/layui.css" media="all">
  <title>Document</title>

  <style>
    .layui-table-cell {
      height: 60px;
      line-height: 60px;
      /* height: auto !important; */
      white-space: normal;
      padding: 0px;
    }

    th .layui-table-cell {
      height: 30px;
      line-height: 30px;
    }

    th .laytable-cell-1-0-7 {
      height: 30px;
      line-height: 30px;
    }

    .layui-table {
      text-align: center;
    }

    .layui-table img {
      width: 100px;
      height: 60px;
    }
  </style>
</head>

<body>

  <fieldset class="table-search-fieldset">
    <legend>搜索信息</legend>
    <div style="margin: 10px 10px 10px 10px">
      <form class="layui-form layui-form-pane" action="">
        <div class="layui-form-item">
          <div class="layui-inline" >
            <label class="layui-form-label">房间号</label>
            <div class="layui-input-inline">
              <input type="text" name="roomId" autocomplete="off" class="layui-input">
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-inline">
              <input type="text" name="status" autocomplete="off" class="layui-input">
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">类型</label>
            <div class="layui-input-inline">
              <input type="text" name="roomType" autocomplete="off" class="layui-input">
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">价格</label>
            <div class="layui-input-inline">
              <input type="text" name="roomPrice" autocomplete="off" class="layui-input">
            </div>
          </div>
          <div class="layui-inline">
            <button type="submit" class="layui-btn layui-btn-normal" lay-submit lay-filter="data-search-btn"><i
                class="layui-icon"> </i> 搜 索</button>
          </div>
        </div>
      </form>
    </div>
  </fieldset>

  <table class="layui-hide" id="test" lay-filter="test"></table>

  <script type="text/html" id="toolbarDemo">
  <div class="layui-btn-container">
    <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
    <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
    <button class="layui-btn layui-btn-normal layui-btn-sm data-export-btn" lay-event="exports"> 导出</button>
  </div>
</script>

  <script type="text/html" id="barDemo">
  <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


  <script src="/layui/layui.js" charset="utf-8"></script>
  <!-- 注意：如果你直接复制所有代码到本地，上述 JS 路径需要改成你本地的 -->

  <script>
    // layui.use() 作用是进行模块加载
    layui.use(['form', 'table'], function () {
      var table = layui.table,
        form = layui.form,
        $ = layui.jquery;
      table.render({
        elem: '#test'
        , url: 'http://localhost:8080/room/pageData'
        , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
        , defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
          title: '提示'
          , layEvent: 'LAYTABLE_TIPS'
          , icon: 'layui-icon-tips'
        }]
        , title: '房间数据表'
        , contentType: "application/x-www-form-urlencoded"
        , cols: [[
          { type: 'checkbox', fixed: 'left', width: '11%' }
          , { field: 'id', title: 'ID', width: '11%', fixed: 'left', unresize: true, sort: true, align: 'center' }
          , { field: 'roomId', title: '房间号', width: '11%', edit: 'text', align: 'center' }
          , { field: 'floor', title: '层数', width: '11%', edit: 'text', sort: true, align: 'center' }
          , { field: 'status', title: '状态', width: '11%', align: 'center' }
          , { field: 'size', title: '面积', align: 'center', width: '11%' }
          , { field: 'roomType', title: '类型', width: '12%', sort: true, align: 'center' }
          , { field: 'roomPrice', title: '价格', width: '11%', align: 'center' }
          , { fixed: 'right', title: '操作', toolbar: '#barDemo', width: '12%', align: 'center' }
        ]],
        page: true
      });


      // 监听搜索操作
      form.on('submit(data-search-btn)', function (data) {
        var result = JSON.stringify(data.field);
        layer.alert(result, {
          title: '最终的搜索信息'
        });

        //执行搜索重载
        table.reload('test', {
          page: {
            curr: 1
          }
          , where: {
            // searchParams: result
            "roomId": data.field.roomId,
            "status": data.field.status,
            "roomType": data.field.roomType,
            "roomPrice": data.field.roomPrice
          }
        }, 'data');

        return false;
      });


      //头工具栏事件
      table.on('toolbar(test)', function (obj) {
        var checkStatus = table.checkStatus(obj.config.id);
        switch (obj.event) {
          case 'getCheckData':
            var data = checkStatus.data;
            layer.alert(JSON.stringify(data));
            break;
          case 'getCheckLength':
            var data = checkStatus.data;
            layer.msg('选中了：' + data.length + ' 个');
            break;
          case 'isAll':
            layer.msg(checkStatus.isAll ? '全选' : '未全选');
            break;
          case 'add':
            layer.open({
              title: '添加房间',
              shade: 0.2,
              type: 2,
              shadeClose: true,   // 点别的地方 是否关闭当前 layer    true收关闭   false 不关闭
              area: ['80%', '100%'],
              fixed: true, //不固定
              maxmin: true,
              content: './roomadd.html',
              end: function (index) {
                table.reload('test');
              }
            });
            break;
          case 'delete':
            var checkStatus = table.checkStatus('test')
              , data = checkStatus.data;
            layer.alert(JSON.stringify(data));
            layer.confirm('真的删除么', function (index) {

              $.ajax({
                "url": "http://localhost:8080/room/multiDelete",
                "method": "post",
                "data": JSON.stringify(data),
                "contentType": "application/json;charset=utf-8",
                "success": function (result) {
                  if (result.code) {
                    layer.msg("删除成功");
                    // 此方式可进行重载，参数只需要table的id或者layui-filter即可
                    table.reload("test", {});
                  } else {
                    layer.msg(result.msg);
                  }
                }
              })

              layer.close(index);
            });

            break;
          //自定义头工具栏右侧图标 - 提示
          case 'LAYTABLE_TIPS':
            layer.alert('这是工具栏右侧自定义的一个图标按钮');
            break;
        };



      });

      //监听行工具事件
      table.on('tool(test)', function (obj) {
        var data = obj.data;
        console.log(obj)
        if (obj.event === 'del') {
          layer.confirm('真的删除行么', function (index) {

            $.ajax({
              "url": "http://localhost:8080/room/delete",
              "data": { "id": data.id },
              // 在这里function() 的参数，可以是任意参数名，服务器回应过来的数据会自动映射上去，所以参数名可以是change，也可以是result
              "success": function (change) {
                if (change.code == 200) {
                  layer.msg("删除成功");
                  obj.del();
                } else {
                  layer.msg("删除失败");
                }
              }
            })

            layer.close(index);
          });
        } else if (obj.event === 'edit') {
          // layer.prompt({
          //   formType: 2
          //   ,value: data.email
          // }, function(value, index){
          //   obj.update({
          //     email: value
          //   });
          //   layer.close(index);
          // });

          // 把数据存到 localStorage, 请求至下一个页面，可以从localStorage中取出数据，localStorage的作用与session的作用类似，是一种传递性的存储工具
          localStorage.setItem("roomManager", JSON.stringify(data));
          layer.open({
            title: '编辑房间',
            shade: 0.2,
            type: 2,
            shadeClose: true,   // 点别的地方 是否关闭当前 layer    true收关闭   false 不关闭
            area: ['80%', '100%'], // 指定修改悬浮窗的大小
            fixed: true, //是否绝对固定，即不随鼠标垫的滑动而上移不见，在指定层位置停留
            maxmin: true,
            content: './roomedit.html', // 修改页面悬浮窗的地址
            end: function (index) {
              table.reload('test'); // 页面行操作修改执行完毕后，进行页面刷新
            }
          });




        }
      });
    });
  </script>
</body>

</html>